Imports System.Data
Imports System.IO

Partial Class WebForms_CInfo_Att
    Inherits WebFormCommon

    Private strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage

        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
            DataGrid.Columns(DataGrid.Columns.Count - 1).Visible = False
            Attach_btn.Visible = False
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Call InitVars()

            Policy_td.InnerText = ViewState("PolicyNo")
            Provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()

            Call LoadAttachments()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Attach_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Attach_btn, Secure.ControlTypes.EditType)
            End If

            sec.AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 1, Secure.ControlTypes.DeleteType)

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select
    End Sub

    Private Sub LoadAttachments()
        Dim dao As New DAOMain.POLICY_CLIENT_ATTACH_DAO
        Dim vo As New DAOMain.POLICY_CLIENT_ATTACH_VO

        vo.POLICY_CLIENT_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        DataGrid.DataSource = dt
        DataGrid.DataBind()

        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Attach_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Attach_btn.Click
        Dim dao As New DAOMain.POLICY_CLIENT_ATTACH_DAO
        Dim vo As New DAOMain.POLICY_CLIENT_ATTACH_VO
        Dim cFunc As New PolyFunc.ASPFunc

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .POLICY_CLIENT_CD = ViewState("CID")

            Select Case lblID.Text
                Case ""
                    Dim dt As DataTable = dao.ExecQuery("Select isnull(max(POLICY_ATTACH_CD),0) +1 as ID From POLICY_CLIENT_ATTACH")
                    .POLICY_ATTACH_CD = dt.Rows(0).Item("ID")
                    .FILENAME = SaveFileName(vo)
                    .CAPTION = Caption_txt.Text
                    .POLICY_ATTACH_CREA_BY = Session("UserName")
                    .POLICY_ATTACH_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        cFunc.MessageAlert(ParentPage, "File Saved!")
                    Else
                        cFunc.MessageAlert(ParentPage, "Error Saving File!")
                    End If
                Case Else
                    .POLICY_ATTACH_CD = lblID.Text
                    .FILENAME = SaveFileName(vo)
                    .CAPTION = Caption_txt.Text
                    .POLICY_ATTACH_LAST_UPD_BY = Session("UserName")
                    .POLICY_ATTACH_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        cFunc.MessageAlert(ParentPage, "File Saved!")
                    Else
                        cFunc.MessageAlert(ParentPage, "Error Saving File!")
                    End If
            End Select
        End With

        Caption_txt.Text = ""
        Call LoadAttachments()

        dao = Nothing
        vo = Nothing
        cFunc = Nothing
    End Sub

    Private Function SaveFileName(ByVal vo As DAOMain.POLICY_CLIENT_ATTACH_VO) As String
        Dim ReturnFile As String = ""
        Dim FileExtn As String = GetFileExtension(FileUpload.FileName)

        Dim strFile As String = "policy_client_" & _
                    vo.POLICY_SEQ_CD & "_" & vo.POLICY_CLIENT_CD & "_" & vo.POLICY_ATTACH_CD & FileExtn

        If File.Exists(strFile) Then File.Delete(strFile)
        FileUpload.SaveAs(Server.MapPath(Request.ApplicationPath & "/File_Attach/" & strFile))
        ReturnFile = Request.ApplicationPath & "/File_Attach/" & strFile

        Return ReturnFile
    End Function

    Private Function GetFileExtension(ByVal FileName As String) As String
        Dim strReturn As String = ""

        Dim i As Integer

        For i = 1 To Len(FileName)
            If Mid(FileName, i, 1) = "." Then
                strReturn = Mid(FileName, i, Len(FileName))
            End If
        Next

        Return strReturn
    End Function

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        Select Case e.CommandName
            Case "View"
                Response.Redirect(e.Item.Cells(1).Text)
            Case "Delete"
                Dim dao As New DAOMain.POLICY_CLIENT_ATTACH_DAO
                Dim vo As New DAOMain.POLICY_CLIENT_ATTACH_VO

                vo.POLICY_CLIENT_CD = ViewState("CID")
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                vo.POLICY_ATTACH_CD = e.Item.Cells(0).Text

                If dao.Delete(vo) Then
                    File.Delete(MapPath(e.Item.Cells(1).Text))
                End If

                Call LoadAttachments()
                dao = Nothing
                vo = Nothing
        End Select
    End Sub

    Protected Sub Main_Info_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Main_Info_btn.Click
        Call InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo.aspx")
    End Sub

    Protected Sub Other_Info_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Other_Info_btn.Click
        Call InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Other.aspx")
    End Sub

    Protected Sub Risk_Profile_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Risk_Profile_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Risk.aspx")
    End Sub

    Protected Sub Correspondence_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Correspondence_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Cor.aspx")
    End Sub
End Class
